/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.prototype.vaadin;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.Callable;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author namounis
 */
public class SvnJobThread implements Callable<HashMap<String,String>>{
    private String pageLink=""; 
    private HashMap<String,String> listCli;
    
    public SvnJobThread(String pageLink){
        this.pageLink = pageLink;
        this.listCli = new HashMap<String,String>();
    }
    
         /* Etape 2 : recupére la ligne de commande selon la structure de la page */  
    public String getCli(String pageLink) throws IOException{
        
        String cli = "";
        
        Document sf_page = Jsoup.connect(pageLink).timeout(0).get();
        
        /* cas 1 */
        Elements result = sf_page.select("div .grid-16 code");
        
        if(!result.text().equals("")){
            cli = result.text();
        }

        /* cas 2 */       
        Elements result1 = sf_page.select("div .yui-u code");
        
        if(!result1.text().equals("")){
            cli = result1.text();
        } 
        
        /* cas 3 */
        Elements result2 = sf_page.select("div .btn-set a");
        
        if(!result2.text().equals("")){
            cli = result2.attr("data-url");
        }
        
        return cli;

    }
    
    public void addListCli(String key, String value){
        this.listCli.put(key, value);
    }
    
    /* Etape 1 : recupére le lien de la page qui contient la ligne de commande */  
    public HashMap<String,String> getCodeURL() throws IOException{

        Document sf_page = Jsoup.connect(this.pageLink).timeout(0).get();
        
        int i = 0;
        int size = sf_page.select("li a").size();
        
        while(i < size){
            Element result = sf_page.select("li a").get(i);
            
              if(result.text().toString().contains("Code")){
                  addListCli(result.text(), getCli(result.absUrl("href")));
              }
              
              if(result.text().toString().equals("Svn")){
                  addListCli(result.text(), getCli(result.absUrl("href")));
              }
              
              if(result.text().toString().equals("Cvs")){ 
                  addListCli(result.text(), getCli(result.absUrl("href")));
              } 
              
              if(result.text().toString().equals("Git")){
                  addListCli(result.text(), getCli(result.absUrl("href")));
              }
              i++;
        }
        return this.listCli;
    }
   
    
    @Override
    public HashMap<String,String> call() throws Exception {
        
        this.listCli = getCodeURL();
        
        return this.listCli;
    }
    
    
}
